package edu.ccut.saturn.studio.dialog.transformdialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnContent;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnTransformNode;

public class SaturnTransformDialogForAdd extends TitleAreaDialog {

	private Shell shell;

	private Text IDText, expressionText;

	private Combo TypeCombo;

	private Label browser;

	public int id;

	private String Idstr = ""; 

	private String Typestr = ""; 

	private String Typevaluekeystr = ""; 

	private List<SaturnContent> saturnContentList = new ArrayList<SaturnContent>();

	private SaturnTransformNode saturnTransformNode;

	public SaturnTransformDialogForAdd(Shell parentShell,
			List<SaturnContent> saturnContentList,
			SaturnTransformNode saturnTransformNode) {
		super(parentShell);
		this.saturnContentList = saturnContentList;
		this.saturnTransformNode = saturnTransformNode;
	}

	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("SaturnTransformDialogForAdd.AddTransformPara"));  //$NON-NLS-1$
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		setTitleImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));

		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		final Label idLabel = new Label(container, SWT.NONE | SWT.READ_ONLY);
		final GridData gd_idLabel = new GridData();
		gd_idLabel.verticalIndent = 15;
		idLabel.setLayoutData(gd_idLabel);
		idLabel.setText(Messages.getString("SaturnTransformDialogForAdd.ParaID"));  //$NON-NLS-1$

		IDText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_idtext = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_idtext.horizontalIndent = 10;
		gd_idtext.verticalIndent = 7;
		gd_idtext.widthHint = 301;
		gd_idtext.horizontalSpan = 2;
		IDText.setLayoutData(gd_idtext);
//		IDText.setText(Messages.getString("SaturnTransformDialogForAdd.12") + (saturnContentList.size() + 1)); //$NON-NLS-1$
		IDText.setText(saturnContentList.size() + 1 + "");
		
		final Label typeLabel = new Label(container, SWT.NONE);
		final GridData gd_typeLabel = new GridData();
		gd_typeLabel.verticalIndent = 15;
		typeLabel.setLayoutData(gd_typeLabel);
		typeLabel.setText(Messages.getString("SaturnTransformDialogForAdd.type")); //$NON-NLS-1$

		TypeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_typeCombo = new GridData(SWT.FILL, SWT.CENTER, false,
				false);
		gd_typeCombo.horizontalIndent = 10;
		gd_typeCombo.verticalIndent = 7;
		gd_typeCombo.horizontalSpan = 2;
		TypeCombo.setLayoutData(gd_typeCombo);

		TypeCombo.setItems(SelectionCandidate.transformClassTypes);
		TypeCombo.select(0);

		final Label typevaluekeyLabel = new Label(container, SWT.NONE);
		final GridData gd_typevaluekeyLabel = new GridData();
		gd_typevaluekeyLabel.verticalIndent = 10;
		typevaluekeyLabel.setLayoutData(gd_typevaluekeyLabel);
		typevaluekeyLabel.setText(Messages.getString("SaturnTransformDialogForAdd.expression"));  //$NON-NLS-1$

		expressionText = new Text(container, SWT.BORDER);
		expressionText.setTextLimit(150);
		final GridData gd_typevaluekeytext = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_typevaluekeytext.horizontalIndent = 10;
		gd_typevaluekeytext.verticalIndent = 7;
		expressionText.setLayoutData(gd_typevaluekeytext);

		browser = new Label(container, SWT.BORDER);
		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS)); 
			}

			public void mouseExit(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); 
			}
		});
		final GridData gd_imageLabel = new GridData(SWT.FILL, SWT.CENTER,
				false, false);
		gd_imageLabel.verticalIndent = 7;
		gd_imageLabel.horizontalIndent = 1;
		browser.setLayoutData(gd_imageLabel);
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				ElementListSelectionDialog dialog1 = new ElementListSelectionDialog(
						shell, new SaturnSWTUtils.SaturnLabelDefaultProvider());
				dialog1.setTitle(Messages.getString("SaturnTransformDialogForAdd.ChooseParaKeyValue"));  //$NON-NLS-1$
				dialog1.setElements(saturnTransformNode.getContentValueKeys());
				if (dialog1.open() == Window.OK) {
					String ddd = (String) dialog1.getFirstResult();

					expressionText.setText(ddd);
				}
			}
		});
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); //$NON-NLS-1$

		return area;
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {

			Idstr = IDText.getText();
			Typestr = TypeCombo.getText();
			Typevaluekeystr = expressionText.getText();
		}
		super.buttonPressed(buttonId);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(415, 266);
	}

	// 将文本框值赋值给MethodVO，供主页面取值
	public void addList(List<SaturnContent> typeList) {
		SaturnContent saturnContent = new SaturnContent();
		saturnContent.setId(Idstr);
		saturnContent.setType(Typestr);
		saturnContent.setContentValueKey(Typevaluekeystr);
		typeList.add(saturnContent);
	}

	public String getIdstr() {
		return Idstr;
	}

	public void setIdstr(String idstr) {
		Idstr = idstr;
	}

	public String getTypestr() {
		return Typestr;
	}

	public void setTypestr(String typestr) {
		Typestr = typestr;
	}

	public String getTypevaluekeystr() {
		return Typevaluekeystr;
	}

	public void setTypevaluekeystr(String typevaluekeystr) {
		Typevaluekeystr = typevaluekeystr;
	}

	protected int getShellStyle() {
		return SWT.RESIZE | SWT.CLOSE;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(SelectionCandidate.ATTRIBUTE_SETTING);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME)); //$NON-NLS-1$
	}
}
